# 文件处理相关类

def print_file_info(file_name):
    """
    接收传入文件的路径，打印文件的全部内容，如文件不存在则捕获异常，输出提示信息，通过finally关闭文件对象
    :param file_name:
    :return:
    """
    file = None
    try:
        file = open(file_name, "r", encoding="UTF-8")
        return file.read()
    except Exception as ex:
        print("有异常：异常原因", ex)
    finally:
        if file:
            file.close()


def append_to_file(file_name, data):
    """
       接收文件路径以及传入数据，将数据追加写入到文件中
    :param file_name:
    :param data:
    :return:
    """
    try:
        val = str(data)
        with open(file_name, "a", encoding="UTF-8") as file:
            file.write(val)
    except Exception as ex:
        print("有异常：异常原因", ex)
    else:
        print("数据添加成功:", print_file_info(file_name))



if __name__ == '__main__':
    val1 =print_file_info("C:/Users/13487/Desktop/aaa.json")
    append_to_file("C:/Users/13487/Desktop/abc.json",val1)

